// pages/evaluate/backhander.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths: [],
    textarea: '',
    comment_id:'',
    eval_img: [],
    order_id:'',
    detail:'',
    order_comments:''
  },
  // 评价内容
  textarea(e) {
    console.log(e) 
    this.setData({
      textarea: e.detail.value
    })
  },
  // 获取商品信息
  item_detail(order_id) {
    let that = this;
    wh.Post('/api/mall.order/item_detail', {
      order_id: order_id
    }, function (res) {
      console.log(res)
      that.setData({
        detail: res.goods[0]
      })
    })
  },
  // 获取用户订单评价
  order_comments(order_id) {
    let that = this;
    wh.Post('/api/mall.comment/order_comments', {
      order_id: order_id
    }, function (res) {
      console.log(res)
      that.setData({
        order_comments: res
      })
    })
  },
  /**
 * 删除图片
 */
  deleteImg: function (event) {
    console.log(event)
    let idx = event.target.dataset.idx, //选中的图片
      tempFilePaths = this.data.tempFilePaths,
      eval_img = this.data.eval_img;
    tempFilePaths.splice(idx, 1);
    eval_img.splice(idx, 1);
    this.setData({
      tempFilePaths: tempFilePaths,
      eval_img: eval_img
    })
  },
  // 上传图片
  uploading() {
    let that = this,
      eval_img = this.data.eval_img,
      tempFilePaths = this.data.tempFilePaths;
    wx.chooseImage({
      count: 3, // 默认9
      success: function (res) {
        if (tempFilePaths.length <= 2) {
          tempFilePaths = that.data.tempFilePaths.concat(res.tempFilePaths)
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            wh.Wxupload('/api/mall.comment/upload_images', res.tempFilePaths[i], 'file').then(function (res) {
              eval_img.push(res)
              that.setData({
                eval_img: eval_img
              })
            })
          }
          that.setData({
            tempFilePaths: tempFilePaths,
            eval_img: eval_img
          })
          return;
        } else {
          wx.showToast({
            title: '最多上传三张哦',
          })
        }
      }
    })
  },
  // 提交评价
  btn() {
    var that = this,
      comment_id = this.data.comment_id;
    if (that.data.textarea.length < 8) {
      wx.showToast({
        title: '评价不能少于8个字',
        icon: "none"
      })
      return false;
    }
    wh.Post('/api/mall.comment/append_comment', {
      comment_id: that.data.comment_id,
      content: that.data.textarea,
      eval_img: that.data.eval_img
    }, function (res) {
      console.log(res)
      wx.showToast({
        title: '评论成功',
        icon: 'none',
        duration: 2000,
        complete: function () {
          setTimeout(function () {
            wx.navigateBack({
              delta: 2
            })
          }, 2000);
        }
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.item_detail(options.order_id)
    this.order_comments(options.order_id)
    this.setData({
      order_id: options.order_id,
      comment_id: options.comment_id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})